import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
/**
 * Generated class for the Userpassedit page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
	selector: 'page-userpassedit',
	templateUrl: 'userpassedit.html',
})
export class UserpasseditPage {
	public userInfo = {
		oldpass: '',
		newpass1: '',
		newpass2: ''
	};

	constructor(
		public navCtrl: NavController,
		public navParams: NavParams,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider) {
	}

	ionViewDidLoad() {
			console.log('ionViewDidLoad Userpassedit');
		}
		/*保存数据*/
	saveOnSuccess() {
		if(this.saveCheck()) {
			this.storage.getStorage("user").then(val => {
				//请求方式如下
				let params = {
					user_id: val['user_id'],
					oldpass: this.userInfo.oldpass,
					newpass: this.userInfo.newpass1
				};
				this.http.postJson('user', 'domodifypass.do', params).then(res => {
					if(res['success'] == true) {
						//关闭modal
						this.navCtrl.pop();
						//修改成功
						this.dialog.showToast("top", "密码修改成功");
					} else {
						this.dialog.showToast("top", res['msg']);
					}
				});
			});
		}
	}

	saveCheck() {
		if(this.userInfo.oldpass == "") {
			this.dialog.showAlert("提示", "请输入原始密码");
			return false;
		}
		if(this.userInfo.newpass1 == "") {
			this.dialog.showAlert("提示", "请输入新密码");
			return false;
		}
		if(this.userInfo.newpass2 == "") {
			this.dialog.showAlert("提示", "请输入确认密码");
			return false;
		}
		if(this.userInfo.newpass1 != this.userInfo.newpass2) {
			this.dialog.showAlert("提示", "新密码和确认密码不一致");
			return false;
		}
		return true;
	}
}